﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_FrontEnd_Menu : System.Web.UI.UserControl
{
   

    private string GetCss(string cate)
    {
        var link = Request.Url.AbsoluteUri.ToLower();        
        if (link.ToLower() == cate.ToLower()) return "active";
        if (link.Contains(cate.ToLower())) return "active";
        return " item";
    }

    public string GenderMenu()
    {
        var html = " <li class=\"first-item\"><a title=\"Trang Chủ\" id=\"menusys1\" class=\" " + GetCss("default") +
                   "\" href=\"" + Business.Config.ApplicationUrl + "\">" +
                   " <span class=\"menusys_name\">Trang Chủ</span></a>" + Environment.NewLine +
                   "</li><li class=\"\"><a title=\"Giới Thiệu\"" +
                   "id=\"menusys69\" class=\" " + GetCss("about") + "\" href=\"" + Business.Config.ApplicationUrl +
                   "about.html\">" +
                   "<span class=\"menusys_name\">Giới Thiệu</span></a></li>" + Environment.NewLine;
        var list = new Business.NewsCategories().NewsCategories_GetByCondition("fatherid=16 or fatherid in (select cateid from NewsCategories where fatherid=16)");
        var listtab = list.FindAll(m => m.FatherID == 16);
        listtab.Sort((m1, m2) => m1.Order.CompareTo(m2.Order));
        foreach (var newsCategoriese in listtab)
        {
            var unocode = ConvertUnicode(newsCategoriese.Name);
            html +=
                "<li class=\" hasChild\"><a title=\"" + newsCategoriese.Name + "\" id=\"menusys" +
                newsCategoriese.CateID + "\" class=\" " + GetCss(unocode) + "\" href=\"" + Business.Config.ApplicationUrl +
             unocode + ".html\"><span class=\"menusys_name\">" + newsCategoriese.Alias +
                "</span></a>" + Environment.NewLine;
            var listsub = list.FindAll(m => m.FatherID == newsCategoriese.CateID);
            listsub.Sort((m1, m2) => m1.Order.CompareTo(m2.Order));
            if (listsub.Count > 0)
                html += "<ul style=\"height: " + listsub.Count * 30 + "px; left: -999em;\">" + Environment.NewLine;
            var indexsub = 0;
            foreach (var categoriese in listsub)
            {
                html +=
                    " <li class=\"" + (indexsub == 0 ? "first" : "last") + "-item\"><a title=\"" + categoriese.Name +
                    "\" id=\"menusys" + categoriese.CateID + "\" class=\" item\" href=\"" +
                    Business.Config.ApplicationUrl + unocode + "/" + ConvertUnicode(categoriese.Name) + ".html\">" +
                    "<span class=\"menusys_name\">" + categoriese.Name + "</span></a></li>" + Environment.NewLine;
                indexsub++;
            }
            if (listsub.Count > 0)
                html += "</ul>";
            html += "</li>" + Environment.NewLine;

        }
        html += "<li class=\"last-item\"><a title=\"Liên hệ\" id=\"menusys87\" class=\"  " + GetCss("contact") + "\" href=\"" +
                   Business.Config.ApplicationUrl + "contact.html\">" +
                   "<span class=\"menusys_name\">Liên hệ</span></a></li>" + Environment.NewLine;
        return html;
    }

    public static String ConvertUnicode(String sContent)
    {
        sContent = sContent.Trim().ToLower();
        const string sUtf8Lower = "a|á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ|đ|e|é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|i|í|ì|ỉ|ĩ|ị|o|ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|u|ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|y|ý|ỳ|ỷ|ỹ|ỵ";

        const string sUcs2Lower = "a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|d|e|e|e|e|e|e|e|e|e|e|e|e|i|i|i|i|i|i|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|o|u|u|u|u|u|u|u|u|u|u|u|u|y|y|y|y|y|y";

        var aUtf8Lower = sUtf8Lower.Split(new[] { '|' });

        var aUcs2Lower = sUcs2Lower.Split(new[] { '|' });

        var l = aUtf8Lower.GetUpperBound(0);

        for (var i = 1; i < l; i++)
            sContent = sContent.Replace(aUtf8Lower[i], aUcs2Lower[i]);
        sContent = sContent.Replace(" - ", " ");
        sContent = sContent.Replace(" ", "-");

        const string filter = "-0123456789abcdefghijklmnopqrstuvwxyz";
        var s = "";
        l = sContent.Length;
        for (var i = 0; i < l; i++)
        {
            if (filter.IndexOf(sContent[i]) >= 0)
            {
                s = s + sContent[i];
            }
        }

        return s;
    }
}